﻿using RpcClient;
using RpcHelper;
using RpcManageClient;
using RpcModel;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
    internal class ServerTransmitCollect : BasicCollect<ServerTransmitConfigDAL>, IServerTransmitCollect
    {
        private readonly IRpcServerCollect _RpcServer;
        public ServerTransmitCollect(IRpcServerCollect server)
        {
            this._RpcServer = server;
        }
        public void Set(ServerTransmitConfig config, TransmitSetParam set)
        {
            if (set.IsEquals(config))
            {
                return;
            }
            TransmitSet param = set.ConvertMap<TransmitSetParam, TransmitSet>();
            param.VerNum = Tools.GetRandom();
            base.BasicDAL.Set(config.Id, param);
            if (set.IsEnable)
            {
                this._RpcServer.RefreshTransmit(config.RpcMerId, config.SystemTypeId);
            }
        }
        public void SetIsEnable(ServerTransmitConfig config, bool isEnable)
        {
            if (config.IsEnable == isEnable)
            {
                return;
            }
            base.BasicDAL.SetIsEnable(config.Id, isEnable);
            this._RpcServer.RefreshTransmit(config.RpcMerId, config.SystemTypeId);
        }
        public long Add(ServerTransmitConfig add)
        {
            add.VerNum = Tools.GetRandom();
            long id = base.BasicDAL.Add(add);
            if (add.IsEnable)
            {
                this._RpcServer.RefreshTransmit(add.RpcMerId, add.SystemTypeId);
            }
            return id;
        }
        public ServerTransmitConfig Get(long id)
        {
            ServerTransmitConfig config = base.BasicDAL.Get(id);
            if (config == null)
            {
                throw new ErrorException("rpc.store.transmit.not.find");
            }
            return config;
        }

        public TransmitBasicConfig[] Query(TransmitQueryParam query, IBasicPage paging, out long count)
        {
            return base.BasicDAL.Query(query, paging, out count);
        }
        public void Delete(ServerTransmitConfig config)
        {
            base.BasicDAL.Delete(config.Id);
            this._RpcServer.RefreshTransmit(config.RpcMerId, config.SystemTypeId);
        }

        public void CheckRepeat(long rpcMerId, long systemType, string scheme)
        {
            if (base.BasicDAL.CheckRepeat(rpcMerId, systemType, scheme))
            {
                throw new ErrorException("rpc.store.transmit.repeat");
            }
        }

        public void CheckRepeat(long rpcMerId, string scheme, long severId)
        {
            if (base.BasicDAL.CheckRepeat(rpcMerId, scheme, severId))
            {
                throw new ErrorException("rpc.store.transmit.repeat");
            }
        }
    }
}
